<?php 
include("lib/facebook.php");
//include("lib/ArrayToXml.php");



//"2.cCVM1g9rheIFWPSqEc_D6g__.86400.1263146400-1049931453";



//init function for redirect to call function
function request_geteway($request = array("method"=>"unknow_method"))
{	
	global $facebook; 
	$appapikey = '3e99105ed5c922110fb95eeb507e1599';
	$appsecret = 'fa104a2e1f7d83328599406adbe7cbf1';
	if(!isset($request['session_key']))
	{
		echo "unknow session key";
		return;
	}
	$facebook = new Facebook($appapikey, $appsecret);
	$facebook->api_client->session_key =  $request['session_key'];
	$facebook->api_client->users_getLoggedInUser();
	$method = $request['method'];
	
	$html="";
	switch($method)
		{
		
		case "get.freinds" :$html = get_friends($request);
		 	break;
		case "get.query" : $html = get_query($request);
			break;
		default :
		    $html = "Unknow method";
		
		
		}
		
		echo $html;

}
 //covert array to xml
function toxml($args =  array(),$method=null)
{
	$xml = "<?xml version='1.0' encoding='utf-8'?>";
	$xml.="<data>";
	if($method)
	{
		$xml.="<method>".$method."</method>";
	}
	foreach($args as  $item)
	{
		$xml.="<item>";
		foreach($item as $key => $data)
		{
			$xml.="<$key>$data</$key>";
		}
		$xml.="</item>";
	
	}
	$xml .='</data>';
	return $xml;

}


function get_friends($param)
{
	global $facebook;
	
	$uid = $facebook->api_client->users_getLoggedInUser();
	$res = $facebook->api_client->fql_query("SELECT name, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 =$uid)");
	
	return toxml($res,$param['method']);
 
}
function get_query($param)
{

	global $facebook;
	$uid = $facebook->api_client->users_getLoggedInUser();
	$res = $facebook->api_client->fql_query($query);
	
	return toxml($res);
	
}
function get_user()
{
	
	
	
}






request_geteway($_REQUEST);





